package com.tegtech.starter.validator.config;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
public class ValidatorConfig {

	@Autowired
	private MessageSource messageSource;

	@Bean
	public Validator validator() {
		LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
		// 设置快速失败，即遇到校验失败后立即返回错误，不再继续校验
		validator.getValidationPropertyMap().put("hibernate.validator.fail_fast", "true");
        // 为Validator配置国际化
		validator.setValidationMessageSource(messageSource);
        return validator;
	}

}
